<script setup>
import FirstTitle from "@comp/FirstTitle";
import { postTableData } from "@/api/table";
import { onMounted } from "vue";
import { searchDataZymb } from "@/api/search";
import { addOrUpdateZymb } from "@/api/addOrUpdate";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";

const city = window.globalObj.name;
const editorRef10 = shallowRef();
const valueHtml10 = ref(`共有XX个重要目标。具体情况如下`);
const toolbarConfig10 = {};
const editorConfig10 = { placeholder: "请输入内容..." };
const mode10 = ref("default");
const handleCreated10 = (editor) => {
  editorRef10.value = editor; // 记录 editor 实例，重要！
};
// 表头单元格样式
const headerCellStyle = () => {
  return {
    backgroundColor: "#f5f7fa",
    color: "rgba(0,0,0,0.85)",
    fontSize: "14px",
    height: "54px",
  };
};
// 单元格样式
const cellStyle = () => {
  return {
    color: "rgba(0,0,0,0.65)",
    fontSize: "14px",
    lineHeight: "22px",
  };
};

// 重要目标分布情况统计表
const zymbList = ref([]);
const getZymbData = () => {
  return postTableData({
    docType: "targetProtection",
    method: "zhongyaomubiaofenbuqingkuang",
  }).then((res) => {
    if (res.code === 200) {
      zymbList.value = res?.data || [];
    }
  });
};
// 加载html
const initHtml = () => {
  searchDataZymb({ m: "任务需求和能力现状目标分布" }).then((res) => {
    const { data } = res;
    valueHtml10.value = data.find((x) => x.k === "目标分布")?.v || "";
  });
};
const saveProgramme = () => {
  ElMessageBox.confirm(`确定要保存吗?`, "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    customClass: "message-box",
  }).then(() => {
    addOrUpdateZymb({
      m: "任务需求和能力现状目标分布",
      data: [
        {
          k: "目标分布",
          v: valueHtml10.value,
        },
      ],
    })
      .then(() => {
        ElMessage.success("操作成功");
      })
      .catch(() => {});
  });
};

onMounted(async () => {
  initHtml();
  await getZymbData();
});
onBeforeUnmount(() => {
  if (editorRef10.value) editorRef10.value.destroy();
});
</script>
<template>
  <div>
    <FirstTitle name="目标分布" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef10"
        :defaultConfig="toolbarConfig10"
        :mode="mode10"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml10"
        :defaultConfig="editorConfig10"
        :mode="mode10"
        @onCreated="handleCreated10"
      />
    </div>
    <div class="top-15 text-center">{{ city }}重要目标分布情况统计表</div>
    <el-table
      id="table"
      :data="zymbList"
      class="top-15"
      style="width: 100%"
      :header-cell-style="headerCellStyle"
      :cell-style="cellStyle"
      border
    >
      <el-table-column prop="rowNum" label="序号" width="60" align="center" />
      <el-table-column prop="jd_name" label="行政区划" />
      <el-table-column prop="name" label="目标名称" />
      <el-table-column prop="detailed_address" label="地址" />
      <el-table-column prop="level" label="目标等级" />
      <el-table-column prop="type" label="目标性质" />
      <el-table-column prop="party_name" label="关键部位" />
    </el-table>
    <div class="flex-justify-end top-20">
      <el-button type="primary" class="save-btn" @click="saveProgramme"
        >保存</el-button
      >
    </div>
  </div>
</template>
<style scoped lang="scss"></style>
